package com.sfj.system.utils;

import java.util.*;

/**
 * @author kaka
 * @Date 2025/07/06
 */
public class StringUtils {
    public static boolean isIdInCommaString(String commaStr, String targetId) {
        if (commaStr == null || targetId == null) {
            return false;
        }
        return Arrays.stream(commaStr.split(","))
                .anyMatch(id -> id.trim().equals(targetId));
    }

    public static String addIdToCommaString(String original, String newId) {
        Set<String> ids = new HashSet<>();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(original)) {
            ids.addAll(Arrays.asList(original.split(",")));
        }
        if (org.apache.commons.lang3.StringUtils.isNotBlank(newId)) {
            ids.add(newId.trim());
        }
        return String.join(",", ids);
    }

    //逗号分割相等判断
    public static boolean isSameElements(String str1, String str2) {
        // 处理 null 或空字符串
        if (str1 == null || str2 == null) {
            return str1 == str2; // 同时为 null 才返回 true
        }

        // 按逗号拆分并转为集合（自动去重）
        Set<String> set1 = new HashSet<>(Arrays.asList(str1.split(",")));
        Set<String> set2 = new HashSet<>(Arrays.asList(str2.split(",")));

        // 比较集合内容
        return set1.equals(set2);
    }

}
